from os import pread


class Node:
    def __init__(self, val, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    head, pre = None, None
    def treeToDoublyList(self, root: 'Node') -> 'Node':
        if root is None:
            return root
        self.inorder(root)
        self.head.left = self.pre
        self.pre.right = self.head

        return self.head
        

    def inorder(self, node):
        if node is None:
            return
        self.inorder(node.left)

        if (self.pre is None):self.head=node
        else:
            node.left = self.pre
            self.pre.right = node
        
        self.pre = node

        self.inorder(node.right)

        


